package com.leyu.mall.admin.store.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.leyu.mall.domain.store.StoreBase;
import com.leyu.mall.service.mdm.IMDMCityService;
import com.leyu.mall.service.store.IStoreBaseService;

/**
 * 
 * @author Administrator
 *
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("admin/store")
public class StoreBaseController {
	@Resource(name = "storeBaseServiceImpl")
	private IStoreBaseService storeBaseServiceImpl;
	
	@Autowired
	private IMDMCityService	mdmCityService;

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Model model, StoreBase storeBase) {
		try {
			storeBaseServiceImpl.save(storeBase);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../store";
	}

	@RequestMapping(value = "delete/{storeId}", method = RequestMethod.GET)
	public String delete(Model model, @PathVariable long storeId) {
		try {
			storeBaseServiceImpl.deleteStoreBase(storeId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../../store";
	}

	@RequestMapping(value = "edit/{storeId}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable long storeId) {
		try {
			model.addAttribute("entity",
					storeBaseServiceImpl.getStoreBaseById(storeId));
			model.addAttribute("citylist", mdmCityService.getMDMCityList());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/editstore.html";
	}

	@ExceptionHandler(Exception.class)
	public String exception(Exception e, HttpServletRequest request) {
		request.setAttribute("exception", e);
		return "/error.html";
	}

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String newADLink(Model model) throws Exception {
		model.addAttribute("citylist", mdmCityService.getMDMCityList());
		
		return "admin/newstore.html";
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String index(Model model) throws Exception {
		try {
			model.addAttribute("storelist", storeBaseServiceImpl.getAllStoreBase());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/store.html";
	}
}
